package com.lib.util;

import android.text.TextUtils;

import com.qbw.log.XLog;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lying
 * @company 9zhitx.com
 */

public class StringUtil {
    public static String string(String s) {
        if (TextUtils.isEmpty(s)) {
            return "";
        }
        return s;
    }

    public static int size(String s) {
        return TextUtils.isEmpty(s) ? 0 : s.length();
    }

    public static float numberFloat(String s) {
        try {
            return Float.parseFloat(s);
        } catch (Exception e) {
            e.printStackTrace();
            XLog.e(e);
        }
        return 0f;
    }

    public static double numberDouble(String s) {
        try {
            return Double.parseDouble(s);
        } catch (Exception e) {
            e.printStackTrace();
            XLog.e(e);
        }
        return 0;
    }

    public static int numberInt(String s) {
        try {
            return Integer.parseInt(s);
        } catch (Exception e) {
            e.printStackTrace();
            XLog.e(e);
        }
        return 0;
    }


    public static List<Long> numbersLong(String s, String split, boolean removeEmpty) {
        List<String> numberStrs = strings(s, split, removeEmpty);
        List<Long> numbers = new ArrayList<>();
        for (String num : numberStrs) {
            try {
                numbers.add(Long.parseLong(num));
            } catch (Exception e) {
                numbers.add(0L);
                e.printStackTrace();
                XLog.e(e);
            }
        }
        return numbers;
    }

    public static List<Integer> numbersInt(String s, String split, boolean removeEmpty) {
        List<String> numberStrs = strings(s, split, removeEmpty);
        List<Integer> numbers = new ArrayList<>();
        for (String num : numberStrs) {
            try {
                numbers.add(Integer.parseInt(num));
            } catch (Exception e) {
                numbers.add(0);
                e.printStackTrace();
                XLog.e(e);
            }
        }
        return numbers;
    }

    public static List<String> strings(String s, String split, boolean removeEmpty) {
        List<String> strings = new ArrayList<>();
        if (TextUtils.isEmpty(s)) {
            return strings;
        }
        String[] stringArr = s.split(split);
        for (String sarr : stringArr) {
            if (removeEmpty && TextUtils.isEmpty(sarr)) {
                continue;
            }
            strings.add(sarr);
        }
        return strings;
    }

    public static List<Double> doubles(String s, String split) {
        List<String> strings = strings(s, split, true);
        int size = strings == null ? 0 : strings.size();
        List<Double> doubles = null;
        if (size > 0) {
            doubles = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                doubles.add(numberDouble(strings.get(i)));
            }
        }
        return doubles;
    }

    public static String stringListToString(List<String> strings, String split) {
        StringBuilder sbs = new StringBuilder();
        if (strings != null && !strings.isEmpty() && !TextUtils.isEmpty(split)) {
            for (String s : strings) {
                sbs.append(s).append(split);
            }
            sbs.delete(sbs.lastIndexOf(split), sbs.length());
        }
        return sbs.toString();
    }
}
